// 模拟新闻数据
let newsData = [
    {
        id: 1,
        title: "示例新闻标题1",
        content: "这是示例新闻1的详细内容...",
        publishTime: "2024-03-20 10:00:00"
    },
    {
        id: 2,
        title: "示例新闻标题2",
        content: "这是示例新闻2的详细内容...",
        publishTime: "2024-03-19 15:30:00"
    }
];

document.addEventListener('DOMContentLoaded', () => {
    // 检查登录状态
    if (!localStorage.getItem('isLoggedIn')) {
        window.location.href = 'login.html';
        return;
    }

    const logoutBtn = document.getElementById('logoutBtn');
    const editModal = document.getElementById('editModal');
    const closeModal = document.getElementById('closeModal');
    const editForm = document.getElementById('editForm');
    const newsTableBody = document.getElementById('newsTableBody');

    // 加载新闻列表
    loadNewsList();

    // 退出登录
    logoutBtn.addEventListener('click', (e) => {
        e.preventDefault();
        localStorage.removeItem('isLoggedIn');
        window.location.href = 'login.html';
    });

    // 关闭模态框
    closeModal.addEventListener('click', () => {
        editModal.style.display = 'none';
    });

    // 点击模态框外部关闭
    window.addEventListener('click', (e) => {
        if (e.target === editModal) {
            editModal.style.display = 'none';
        }
    });

    // 编辑新闻
    editForm.addEventListener('submit', (e) => {
        e.preventDefault();
        const newsId = document.getElementById('editNewsId').value;
        const title = document.getElementById('editTitle').value;
        const content = document.getElementById('editContent').value;

        // 更新新闻数据
        const newsIndex = newsData.findIndex(n => n.id === parseInt(newsId));
        if (newsIndex !== -1) {
            newsData[newsIndex].title = title;
            newsData[newsIndex].content = content;
            loadNewsList();
            editModal.style.display = 'none';
        }
    });
});

// 加载新闻列表
function loadNewsList() {
    const newsTableBody = document.getElementById('newsTableBody');
    newsTableBody.innerHTML = newsData.map(news => `
        <tr>
            <td>${news.title}</td>
            <td>${news.publishTime}</td>
            <td>
                <button class="action-btn edit-btn" onclick="editNews(${news.id})">
                    <i class="fas fa-edit"></i> 编辑
                </button>
                <button class="action-btn delete-btn" onclick="deleteNews(${news.id})">
                    <i class="fas fa-trash"></i> 删除
                </button>
            </td>
        </tr>
    `).join('');
}

// 编辑新闻
function editNews(id) {
    const news = newsData.find(n => n.id === id);
    if (news) {
        document.getElementById('editNewsId').value = news.id;
        document.getElementById('editTitle').value = news.title;
        document.getElementById('editContent').value = news.content;
        document.getElementById('editModal').style.display = 'block';
    }
}

// 删除新闻
function deleteNews(id) {
    if (confirm('确定要删除这条新闻吗？')) {
        newsData = newsData.filter(n => n.id !== id);
        loadNewsList();
    }
} 